package com.example.nbb.Controller;

import com.example.nbb.Service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("/nbb/User/")
public class UserController {
    @Resource
    UserService userService;

    /**
     * 用户登录接口
     *
     * @param phone 手机号
     * @param pwd   密码
     * @return 返回
     */
    @GetMapping("login")
    public String login(@RequestParam("phone") String phone, @RequestParam("pwd") String pwd) {
        return userService.login(phone, pwd);
    }

    /**
     * 用户注册接口
     *
     * @param userName   用户名
     * @param userAvatar 用户头像
     * @param userPhone  用户手机号
     * @param userPwd    用户密码
     * @return 返回
     */
    @PostMapping("register")
    public String register(@RequestParam("userName") String userName, @RequestParam("userAvatar") String userAvatar,
                           @RequestParam("userPhone") String userPhone, @RequestParam("userPwd") String userPwd) {
        return userService.register(userName, userAvatar, userPhone, userPwd);
    }

    /**
     * 用户修改密码接口
     *
     * @param id       id
     * @param password 密码
     * @return 返回
     */
    @PostMapping("changePassword")
    public String changePassword(@RequestParam("id") int id, @RequestParam("password") String password) {
        return userService.changePassword(id, password);
    }

    @PostMapping("updateAvatar")
    public String updateAvatar(@RequestParam("id") int id, @RequestParam("file") MultipartFile file) {
        String avatar = userService.upFile(file);
        return userService.updateAvatar(id, avatar);
    }

    @GetMapping("selectAvatar")
    public String selectAvatar(@RequestParam("id") int id) {
        return userService.selectAvatar(id);
    }
}
